//
//  DicSenTableView.m
//  划词显示
//
//  Created by 盛超 on 2018/8/25.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "DicSenTableView.h"
#import "UIView+DicUtils.h"
#import "NSString+DicUtils.h"
#import "SCDict.h"

@interface DicSenTableView ()

@property (nonatomic,strong) NSMutableArray *rowdataArray;
@property (nonatomic,strong) NSMutableArray *sectiondataArray;
@property (nonatomic,strong) NSMutableAttributedString *attrString;
@property (nonatomic,strong) NSMutableArray *heightArr;

@end

@implementation DicSenTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self = [super initWithFrame:frame style:style]) {

        self.estimatedRowHeight = 44;
        self.backgroundColor = [UIColor clearColor];
        self.dataSource = self;
        self.delegate = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self registerClass:[DicSenCell class] forCellReuseIdentifier:@"DicSenCell"];
    }
    return self;
}

#pragma mark - UITableDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *senArr = self.rowdataArray[indexPath.section];
    if (_moudelID == DictColType_Sen) {
      
        sen *senModel = senArr[indexPath.row];
        NSString *string = [NSString stringWithFormat:@"%ld.%@<br>  <font color=\"#515151\">%@</font>",indexPath.row+1,senModel.sentenceEn,senModel.sTranslation];
        DicSenCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DicSenCell"];
        cell.attr = [string dic_HtmlStringWithFontSize:13.0f];
        cell.url = senModel.sViocePath;
        return cell;
    } else if (_moudelID == DictColType_Coll) {
        
        collsen *collSenModel = senArr[indexPath.row];
        NSString *string = [NSString stringWithFormat:@"%ld.%@<font color=\"#515151\">%@</font>",indexPath.row+1,collSenModel.Coll_SenEng,collSenModel.Coll_SenChi];
        DicSenCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DicSenCell"];
        NSAttributedString *attr = [string dic_HtmlStringWithFontSize:13.0f];
        cell.attr = attr;
        
        return cell;
        
    } else if (_moudelID == DictColType_Disc) {
        
        disc *discMdoel = senArr[indexPath.row];
        NSString *string = [NSString stringWithFormat:@"%ld.%@<br>  <font color=\"#515151\">%@</font>",indexPath.row+1,discMdoel.word,discMdoel.Description];
        DicSenCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DicSenCell"];
        cell.attr = [string dic_HtmlStringWithFontSize:13.0f];
        
        return cell;
    } else if (_moudelID == DictColType_Class) {
        classic *classModel = senArr[indexPath.row];
        if ([classModel.ClassicApp hasPrefix:@"<P>"]) {
            classModel.ClassicApp = [classModel.ClassicApp substringFromIndex:3];
        }
        NSString *string = [NSString stringWithFormat:@"%ld.%@",indexPath.row+1,classModel.ClassicApp];
        DicSenCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DicSenCell"];
        cell.attr = [string dic_HtmlStringWithFontSize:13.0f];
        
        return cell;
    }
    
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSArray *senArr = self.rowdataArray[section];
    
    return senArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return  self.sectiondataArray.count;
}

#pragma mark - UITableDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSArray *senArr = self.rowdataArray[indexPath.section];
    if (_moudelID == DictColType_Sen) {
        sen *senModel = senArr[indexPath.row];
        NSString *string = [NSString stringWithFormat:@"%ld.%@<br>  <font color=\"#515151\">%@</font>",indexPath.row+1,senModel.sentenceEn,senModel.sTranslation];
        NSAttributedString *attr = [string dic_HtmlStringWithFontSize:13.0f];
        
        CGSize size= [attr boundingRectWithSize:CGSizeMake(266*dic_ScalesW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
        
        return size.height+10;
    } else if (_moudelID == DictColType_Coll) {
        
        collsen *collSenModel = senArr[indexPath.row];
        NSString *string = [NSString stringWithFormat:@"%ld.%@<font color=\"#515151\">%@</font>",indexPath.row+1,collSenModel.Coll_SenEng,collSenModel.Coll_SenChi];
        
        NSAttributedString *attr = [string dic_HtmlStringWithFontSize:13.0f];
        
        CGSize size= [attr boundingRectWithSize:CGSizeMake(266*dic_ScalesW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
        return size.height+10;
        
    } else if (_moudelID == DictColType_Disc) {
        disc *discMdoel = senArr[indexPath.row];
        NSString *string = [NSString stringWithFormat:@"%ld.%@<font color=\"#515151\">%@</font>",indexPath.row+1,discMdoel.word,discMdoel.Description];
        NSAttributedString *attr = [string dic_HtmlStringWithFontSize:13.0f];
        
        CGSize size= [attr boundingRectWithSize:CGSizeMake(266*dic_ScalesW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
        return size.height+10;
    } else if (_moudelID == DictColType_Class) {
        classic *classModel = senArr[indexPath.row];
        if ([classModel.ClassicApp hasPrefix:@"<P>"]) {
            classModel.ClassicApp = [classModel.ClassicApp substringFromIndex:3];
        }
        NSString *string = [NSString stringWithFormat:@"%ld.%@",indexPath.row+1,classModel.ClassicApp];
        NSAttributedString *attr = [string dic_HtmlStringWithFontSize:13.0f];
        CGSize size= [attr boundingRectWithSize:CGSizeMake(266*dic_ScalesW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
        return size.height+10;
    }
    
    return 0;
    
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.width,60)];
    NSString *string = self.sectiondataArray[section];
   // view.backgroundColor = [UIColor redColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,5,self.width-20, 50)];
    label.attributedText = [string dic_HtmlStringWithFontSize:15.0];
    label.font = [UIFont boldSystemFontOfSize:15.0f];
    label.textColor = [UIColor blackColor];
    [view addSubview:label];
    label.numberOfLines = 0;
//    CGSize size = [label.attributedText boundingRectWithSize:CGSizeMake(self.width-20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
//    label.height = size.height+1;
//    view.height =  label.height + 10;
    
    return view;
}

- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0,self.width,2)];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    NSString *string = self.sectiondataArray[section];
//    NSAttributedString *attributedText = [string dic_HtmlStringWithFontSize:15.0];
//    CGSize size = [attributedText boundingRectWithSize:CGSizeMake(self.width-20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
//
//    return size.height+1+10;
    
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 2;
}

#pragma mark - 数据处理
- (void)setModelDic:(ModelDic *)modelDic {
    _modelDic = modelDic;
    self.rowdataArray = [NSMutableArray array];
    self.sectiondataArray = [NSMutableArray array];
    
    if (_moudelID == DictColType_Sen) {
        for (cx *cxmodel in _modelDic.cxCollection ) {
            NSString *string = cxmodel.cxEnglish;
            for (mean *meanModel in cxmodel.meanCollection) {
                [self.sectiondataArray addObject:[NSString stringWithFormat:@"%@%@",string,meanModel.chineseMeaning]];
                [self.rowdataArray addObject:meanModel.senCollection];
            }
        }
    } else if (_moudelID == DictColType_Coll) {
        for (cx *cxmodel in _modelDic.cxCollection ) {
            
            for (coll *collModel in cxmodel.CollCollection ) {
                [self.sectiondataArray addObject:collModel.Coll_English];
                [self.rowdataArray addObject:collModel.CollSenCollection];
            }
        }
        
    } else if (_moudelID == DictColType_Disc) {
        
        for (cx *cxmodel in _modelDic.cxCollection ) {
            NSString *string = cxmodel.cxEnglish;
          
            if (cxmodel.DiscCollection.count>0) {
                [self.sectiondataArray addObject:string];
                [self.rowdataArray addObject:cxmodel.DiscCollection];
            }
        }

    } else if (_moudelID == DictColType_Class) {

        for (cx *cxmodel in _modelDic.cxCollection ) {
            NSString *string = cxmodel.cxEnglish;
            
            if (cxmodel.ClassCollection.count > 0) {
                [self.sectiondataArray addObject:string];
                [self.rowdataArray addObject:cxmodel.ClassCollection];
            }
        }
        
    }
    
    [self reloadData];
}

@end


@implementation DicSenCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.player =[BKDictAudioPlayer sharedMPlayer];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        self.senlabel = [[TYAttributedLabel alloc] init];
        self.senlabel.backgroundColor = [UIColor clearColor];
        self.senlabel.numberOfLines = 0;
        self.senlabel.lineBreakMode = NSLineBreakByWordWrapping;
        [self addSubview:self.senlabel];
        
        self.btnImageView = [[LSPlaySenBtn alloc] init];
        self.btnImageView.userInteractionEnabled = YES;
        [self.btnImageView setBackgroundImage:[UIImage imageNamed:@"LSDict.bundle/句子读音图标"] forState:UIControlStateNormal];
        [self addSubview:self.btnImageView];

        [self.btnImageView addTarget:self action:@selector(playVoice) forControlEvents:UIControlEventTouchUpInside];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(closeAllPlayer) name:@"LSDictColsePlayer" object:nil];
        
    }
    
    return self;
}

- (void)setAttr:(NSAttributedString *)attr {
    _attr = attr;
    self.senlabel.frame = CGRectMake(10, 0, 266*dic_ScalesW,20);
    self.senlabel.attributedText = _attr;
    [self.senlabel sizeToFit];
}

- (void)setUrl:(NSString *)url {
    _url = url;
    if (_url!=nil) {
        self.btnImageView.frame = CGRectMake(self.senlabel.x+self.senlabel.width+2,3, 15, 13);
    }
}

- (void)playVoice {
    //self.player.url = _url;
    [self.player playerUrl:_url seekTotime:0 rateValue:1];
    [self.player playerPlay];
}

- (void)closeAllPlayer {
    [self.player closePlayer];
}


@end

@implementation LSPlaySenBtn

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
    }
    return self;
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    //当前btn大小
    CGRect btnBounds = self.bounds;
    //扩大点击区域，想缩小就将-10设为正值
    btnBounds = CGRectInset(btnBounds, -10, -10);
    
    //若点击的点在新的bounds里，就返回YES
    return CGRectContainsPoint(btnBounds, point);
}

@end
